package com.systemframework.sms;

import cn.hutool.core.util.RandomUtil;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.systemframework.constants.Constants;
import com.systemframework.exception.ResultException;
import com.systemframework.properties.SmsProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;



/**
 * @author summer
 * @date 2023-06-15 20:51
 **/
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnClass({SmsProperties.class})
@Configuration
@AllArgsConstructor
@Slf4j
public class TencentSms {

    private final SmsProperties smsProperties;



    public void sms(String phone)  {
        String randomNumbers = RandomUtil.randomNumbers(smsProperties.getRandomLength());
        SmsSingleSender smsSingleSender=new SmsSingleSender(smsProperties.getAppId(),smsProperties.getAccessKeyId());
        String[] params = {randomNumbers};
        try {
            SmsSingleSenderResult smsSingleSenderResult = smsSingleSender.sendWithParam("86", phone, smsProperties.getTemplateId(), params, smsProperties.getSignName(), "", "");
            if(smsSingleSenderResult.result== Constants.ZERO){
                log.info("腾讯短信发送成功");
            }else{
                log.error("腾讯短信发送失败");
            }
        }catch (Exception e){
            log.error("腾讯云短信发送失败:{}",e.getMessage(),e);
            throw new ResultException("腾讯云短信发送失败:"+e.getMessage());
        }


    }


}
